Shell

推荐列表 站点导航

当前位置:首页 > 脚本编程 > Shell >

java for each 循环 数组 对象遍历

来源:互联网  作者:网友投稿  发布时间:2021-01-08 20:40
jquery中文网为您提供java for each 循环 数组 对象遍历等资源,欢迎您收藏本站,我们将为您提供最新的java for each 循环...

java for each 循环 数组 对象遍历

语法

for(type itr-var : iterableobj) statement-block

看个遍历数组实例

<blockquote>

public class mainclass {
  public static void main(string args[]) {
    int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
    for(int x : nums) {
      system.out.print(x " ");
      x = x * 10; // no effect on nums
    }
  
    system.out.println();

for(int x : nums)
      system.out.print(x " ");

system.out.println();
  } 
}

</blockquote>


输出

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10


遍历enum对象

<blockquote>

public class mainclass {
  enum season {
    spring, summer, fall, winter
  }

public static void main(string[] args) {
    for (season season : season.values()) {
      system.out.println(" the season is now " season);
    }
  }
}

</blockquote>

结果

the season is now spring
 the season is now summer
 the season is now fall
 the season is now winter

遍历arraylist

<blockquote>

import java.util.arraylist;
 
public class mainclass {
   
  public static void main(string args[]) {
    arraylist<double> list = new arraylist<double>();

list.add(10.14);
    list.add(20.22);
    list.add(30.78);
    list.add(40.46);

double sum = 0.0;
    for(double itr : list)
      sum = sum itr;
    system.out.println(sum);
 
  }
}

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/shell/12063.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

java for each 循环 数组 对象遍历

2021-01-08 编辑:网友投稿

java for each 循环 数组 对象遍历

语法

for(type itr-var : iterableobj) statement-block

看个遍历数组实例

<blockquote>

public class mainclass {
  public static void main(string args[]) {
    int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
    for(int x : nums) {
      system.out.print(x " ");
      x = x * 10; // no effect on nums
    }
  
    system.out.println();

for(int x : nums)
      system.out.print(x " ");

system.out.println();
  } 
}

</blockquote>


输出

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10


遍历enum对象

<blockquote>

public class mainclass {
  enum season {
    spring, summer, fall, winter
  }

public static void main(string[] args) {
    for (season season : season.values()) {
      system.out.println(" the season is now " season);
    }
  }
}

</blockquote>

结果

the season is now spring
 the season is now summer
 the season is now fall
 the season is now winter

遍历arraylist

<blockquote>

import java.util.arraylist;
 
public class mainclass {
   
  public static void main(string args[]) {
    arraylist<double> list = new arraylist<double>();

list.add(10.14);
    list.add(20.22);
    list.add(30.78);
    list.add(40.46);

double sum = 0.0;
    for(double itr : list)
      sum = sum itr;
    system.out.println(sum);
 
  }
}

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/shell/12063.shtml

相关文章

风云图片

推荐阅读

返回Shell频道首页